package com.design.pattern.singleton;

import java.lang.reflect.Constructor;

public class SingletonV3 {



    private SingletonV3() {

        System.out.println("构造函数被初始化...");
    }

    public static SingletonV3 getInstance() {
        return SingletonV3Utils.SingletonV3;
    }


    // 在类里面嵌套的
    private static class SingletonV3Utils {
        private static final SingletonV3 SingletonV3 = new SingletonV3();
    }

    /**
     * 内部类在调用的时候才会初始化SingletonV3
     * static 静态 保证唯一
     * @param args
     */
    // 静态内部类特征：继承懒汉式和饿汉式优点、同时解决双重检验锁第一次加载慢的问题 读和写都不需要同步效率非常高...
    public static void main(String[] args) throws Exception {
        System.out.println("项目启动成功...");
        SingletonV3 instance1 = SingletonV3.getInstance();
        SingletonV3 instance2 = SingletonV3.getInstance();
        System.out.println(instance1 == instance2); // true


        Constructor<SingletonV3> declaredConstructor = SingletonV3.class.getDeclaredConstructor();
        declaredConstructor.setAccessible(true);
        SingletonV3 singletonV3 = declaredConstructor.newInstance();
        System.out.println(instance1 == singletonV3); // true


    }

}
